package com.android.phoenix.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.phoenix.Constant;
import com.android.phoenix.MainApplication;
import com.android.phoenix.R;

/**
 * @ClassName: LoginActivity
 * @Description: 用户登录界面及注册入口
 * @author: wanchao
 * @date: 2014-12-25 下午4:44:22
 */
public class LoginActivity extends BaseActivity implements OnClickListener {
	private TextView txtRegister = null;
	private EditText editLoginPhone = null;
	private EditText editLoginPassword = null;
	private ImageView imgLoginDeletePhone = null;
	private ImageView imgLoginDeletePassword = null;
	private Button btnLogin = null;

	private long back = 0;

	private String txtEditPhone = "";
	private String txtEditPassword = "";

	private boolean loginSucceed;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		loginSucceed = MainApplication.mSharedPreferences.getBoolean(
				Constant.LOGIN_SUCCEED, false);
		Log.i("wanchao", "loginSucceed = " + loginSucceed);
		if (loginSucceed) {
			Intent it = null;
			if (isFirstLogin()) {
				it = new Intent(LoginActivity.this,
						UserInfoImproveActivity.class);
			} else {
				it = new Intent(LoginActivity.this, MainActivity.class);
			}
			startActivity(it);
			LoginActivity.this.finish();
			return;
		}
		setContentView(R.layout.acti_login);
		initViews();
	}

	/**
	 * @Title: initViews
	 * @Description:控件等初始化
	 * @author: wanchao
	 * @date: 2014-12-25 下午4:45:19
	 */
	private void initViews() {
		txtRegister = (TextView) findViewById(R.id.txtRegister);
		editLoginPhone = (EditText) findViewById(R.id.editLoginPhone);
		editLoginPassword = (EditText) findViewById(R.id.editLoginPassword);
		imgLoginDeletePhone = (ImageView) findViewById(R.id.imgLoginDeletePhone);
		imgLoginDeletePassword = (ImageView) findViewById(R.id.imgLoginDeletePassword);
		btnLogin = (Button) findViewById(R.id.btnLogin);

		txtRegister.setOnClickListener(this);
		editLoginPhone.addTextChangedListener(textWatcherPhone);
		editLoginPassword.addTextChangedListener(textWatcherPassword);
		imgLoginDeletePhone.setOnClickListener(this);
		imgLoginDeletePassword.setOnClickListener(this);
		btnLogin.setOnClickListener(this);
		btnLogin.setEnabled(false);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		editLoginPhone.setText(MainApplication.mSharedPreferences.getString(
				Constant.USERNAME, ""));
		editLoginPhone.setSelection(editLoginPhone.getText().length());
	}

	// editLoginPhone内容改变监听
	private TextWatcher textWatcherPhone = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			if (!s.toString().equals("")) {
				txtEditPhone = s.toString();
				imgLoginDeletePhone.setVisibility(View.VISIBLE);
				if (!TextUtils.isEmpty(txtEditPassword)) {
					btnLogin.setEnabled(true);
				}
			} else {
				btnLogin.setEnabled(false);
				txtEditPhone = s.toString();
				imgLoginDeletePhone.setVisibility(View.INVISIBLE);
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
	};
	// editLoginPassword内容改变监听
	private TextWatcher textWatcherPassword = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			if (!s.toString().equals("")) {
				txtEditPassword = s.toString();
				imgLoginDeletePassword.setVisibility(View.VISIBLE);
				if (!TextUtils.isEmpty(txtEditPhone)) {
					btnLogin.setEnabled(true);
				}
			} else {
				btnLogin.setEnabled(false);
				txtEditPassword = s.toString();
				imgLoginDeletePassword.setVisibility(View.INVISIBLE);
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
	};

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.txtRegister:
			Intent it = new Intent(this, RegisterActivity.class);
			startActivity(it);
			overridePendingTransition(R.anim.slide_right_in,
					R.anim.slide_left_out);
			break;
		case R.id.imgLoginDeletePhone:
			editLoginPhone.setText("");
			break;
		case R.id.imgLoginDeletePassword:
			editLoginPassword.setText("");
			break;
		case R.id.btnLogin:
			login();
			break;
		}
	}

	/**
	 * @Title: login
	 * @Description: 进行登录及登录相关信息控制
	 * @author: wanchao
	 * @date: 2014-12-26 下午2:32:24
	 */
	private void login() {
		Intent it = null;
		if (isAccountValid()) {
			MainApplication.mSharedPreferences.edit().putBoolean(
					Constant.LOGIN_SUCCEED, true).commit();
			if (isFirstLogin()) {
				it = new Intent(LoginActivity.this,
						UserInfoImproveActivity.class);
			} else {
				it = new Intent(LoginActivity.this, MainActivity.class);
			}
			startActivity(it);
			overridePendingTransition(R.anim.slide_bottom_in,
					R.anim.slide_top_out);
			LoginActivity.this.finish();
		} else {
			if ("".equals(MainApplication.mSharedPreferences.getString(
					Constant.USERNAME, ""))) {
				RegisterActivity.showMyDialog(this, "提示", "先注册一下吧，亲！");
			} else {
				RegisterActivity.showMyDialog(this, "提示", "帐号或密码错误！");
			}
		}
	}

	/**
	 * @Title: isAccountValid
	 * @Description: 判断输入帐号密码是否正确
	 * @author: WanChao
	 * @date: 2015-1-23 下午11:03:24
	 * @return true 用户名密码正确；false 用户名密码错误；
	 */
	private boolean isAccountValid() {
		String userName = MainApplication.mSharedPreferences.getString(
				Constant.USERNAME, "");
		String password = MainApplication.mSharedPreferences.getString(
				Constant.PASSWORD, "");
		if (userName.equals(editLoginPhone.getText().toString().trim())
				&& password.equals(editLoginPassword.getText().toString()
						.trim())) {
			return true;
		}
		return false;
	}

	/**
	 * @Title: isFirstLogin
	 * @Description: 判断是否为第一次登录
	 * @author: WanChao
	 * @date: 2015-1-7 下午9:59:24
	 */
	private boolean isFirstLogin() {
		return MainApplication.mSharedPreferences.getBoolean(
				Constant.ISFIRSTLOGIN, true);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			long current = System.currentTimeMillis();
			if ((current - back) < 2000) {
				finish();
				System.exit(0);
			} else {
				back = current;
				Toast.makeText(LoginActivity.this, "再按一次退出程序",
						Toast.LENGTH_SHORT).show();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
